package com.aizhixin.lab.project.course.service;

import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.dto.SortDTO;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.project.course.domain.ProjectDailyPaperDomain;
import com.aizhixin.lab.project.course.domain.ProjectMinutesCheckDomain;
import com.aizhixin.lab.project.course.domain.QueryProjectWeeklyDomain;
import com.aizhixin.lab.project.course.entity.ProjectDailyPaper;
import com.aizhixin.lab.project.course.repository.ProjectDailyPaperRepository;
import com.aizhixin.lab.project.course.vo.ProjectDailyPaperVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

@Slf4j
@Service
@Transactional
public class ProjectDailyPaperService {
    @Autowired
    private ProjectDailyPaperRepository projectDailyPaperRepository;
    @Autowired
    private PageJdbcUtil pageJdbcUtil;

    RowMapper<ProjectDailyPaperVo> dailyPaperVoRowMapper = new RowMapper<ProjectDailyPaperVo>() {

        @Override
        public ProjectDailyPaperVo mapRow(ResultSet rs, int rowNum) throws SQLException {

            ProjectDailyPaperVo domain = new ProjectDailyPaperVo();
            domain.setId(rs.getString("id"));
            domain.setCheckStatus(rs.getInt("check_status"));
            domain.setCheckSuggest(rs.getString("check_suggest"));
            domain.setGiveMark(rs.getDouble("give_mark"));
            domain.setCreatedBy(rs.getString("created_by"));
            domain.setCreatedDate(rs.getString("created_date"));
            domain.setCheckSuggest(rs.getString("check_suggest"));
            domain.setTomorrowTask(rs.getString("tomorrow_task"));
            domain.setAccomplishTask(rs.getString("accomplish_task"));
            domain.setTodayReap(rs.getString("today_reap"));
            domain.setName(rs.getString("name"));
            domain.setProblemsAndTactics(rs.getString("problems_and_tactics"));
            return domain;
        }
    };

    public ProjectDailyPaper save(ProjectDailyPaperDomain domain) {
        ProjectDailyPaper projectDailyPaper = new ProjectDailyPaper();
        BeanUtils.copyProperties(domain, projectDailyPaper);
        projectDailyPaper.setId(UUID.randomUUID().toString());
        projectDailyPaper.setCreatedDate(new Date());
        projectDailyPaper.setDeleteFlag(DataValidity.VALID.getState());
        projectDailyPaper.setCheckStatus(0);
        return projectDailyPaperRepository.save(projectDailyPaper);
    }

    public ProjectDailyPaper update(ProjectDailyPaperDomain domain) {
        ProjectDailyPaper projectDailyPaper = projectDailyPaperRepository.findOne(domain.getId());
        BeanUtils.copyProperties(domain, projectDailyPaper);
        return projectDailyPaperRepository.save(projectDailyPaper);
    }

    public ProjectDailyPaper check(ProjectMinutesCheckDomain domain) {
        ProjectDailyPaper projectDailyPaper = projectDailyPaperRepository.findOne(domain.getId());
        BeanUtils.copyProperties(domain, projectDailyPaper);
        projectDailyPaper.setCheckTime(new Date());
        projectDailyPaper.setCheckStatus(1);
        return projectDailyPaperRepository.save(projectDailyPaper);
    }

    public <T> Map<String, Object> findProjectDailyPaper(QueryProjectWeeklyDomain domain) {
        String sql = "SELECT a.id,a.name,a.give_mark,a.accomplish_task,a.today_reap,a.problems_and_tactics,a.tomorrow_task,a.check_status,a.check_suggest,a.created_date,b.NAME as created_by FROM t_project_daily_daper a LEFT JOIN t_user b ON a.created_by = b.id where a.project_id = '" + domain.getProjectId() + "' and a.group_id = '" + domain.getGroupId() + "'";
        String count = "SELECT count(*) FROM t_project_daily_daper a LEFT JOIN t_user b ON a.created_by = b.id where a.project_id = '" + domain.getProjectId() + "' and a.group_id = '" + domain.getGroupId() + "'";
        if (!StringUtils.isEmpty(domain.getCreatedBy())){
            sql+=" and a.created_by="+domain.getCreatedBy();
            count+=" and a.created_by="+domain.getCreatedBy();
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto = new SortDTO();
        dto.setKey("a.check_time");
        dto.setAsc(false);
        sort.add(dto);
        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), dailyPaperVoRowMapper, sort, sql, count);
    }


}
